﻿<?php
$user=$_POST["usernamesignup"];
$email=$_POST["emailsignup"];
$pass=$_POST["passwordsignup"];
$name=$_POST["fullnamesigup"];

function checkEmail($email)
{
  if(preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i", $email) > 0)
  {
    return true;
  }
  else
  {
     return false;
  }
}

if($name == null || $pass == null || checkEmail($email)== false)
{
    echo "Dang Ki That Bai"." ";
    echo "<a href='form.php'>Thu lai</a>";  
}
else
{


        //1. Tao ket noi
        $conn = mysql_connect("localhost", "root","123456") or die("Ket noi den MySQL that bai");
        //2. use database
        mysql_query("SET NAMES UTF8",$conn);
        mysql_select_db("mp3", $conn);//use abc
        // checks if the username is in use
       	if (!get_magic_quotes_gpc()) 
        {

 		     $user = addslashes($user);
             $email = addslashes($email);
        }
        $checkuser = mysql_query("SELECT username FROM user WHERE username = "."'". $user."'") or die(mysql_error());
        $check_email = mysql_query("SELECT email FROM user WHERE email = "."'". $email."'") or die(mysql_error());
        $check = mysql_num_rows($checkuser);
        $check2 = mysql_num_rows($check_email);
        //if the name exists it gives an error
        if ($check2 != 0 || $check != 0) 
        {
           
            echo "<div class='erorr' style='z-index:1000; position:fixed; border-radius:10px; padding:40px; text-align:left; left:400px; top:315px; line-height:2; background-color:#B6B6B6'>";
            echo "<span style=' font-size:20px; font-weight:bold; color: red;'>"."Đăng Kí Thất Bại :("."</span>"."<br/>";
            echo "Sorry, tên đăng nhập"." "."<span style='font-weight:bold; color: red;'>".$user."</span>"." "."hoặc"." "."<span style='font-weight:bold; color: red;'>".$email."</span> "."đã có người sử dụng."."<br/>";
            echo "<a href='form.php#toregister'>Thu lai</a>";
            echo "</div>";
            echo "<div class='opa' style='width:100%; height:100%; background-color:#DFDFDF; opacity:0.6'>";
            echo "</div>";
        }
        else
        {
        //3. thuc thi cau sql
                    $now = getdate();
                    $currentDate = $now["year"]. "." . $now["mon"] . "." . $now["mday"];
    
            $sql = "INSERT INTO `user` (`username`, `pass`, `email`, `fullname`, `ngaytao`)
            "." Values ('".$user."','".$pass."','".$email."','".$name."','".$currentDate."')";
                    echo "<div style='z-index:1000; position:fixed; border-radius:10px; padding:40px; text-align:left; left:400px; top:315px; line-height:2; background-color:#B6B6B6'>";
                    echo "<span style=' font-size:20px; font-weight:bold; color: red;'>"."Đăng Kí Thành Công. Chào Mừng Bạn Đến Với<a href='../Mp3/home.php'> MuSic Is Life </a>"."</span>"."<br/>";
                    echo "</div>";
                    echo "<div style='width:100%; height:100%; background-color:#DFDFDF; opacity:0.6'>";
                    echo "</div>";
                    $q = mysql_query($sql);//4. Lay ket qua tra ve
        }       
        /*C2:"INSERT INTO `user` (`ACC`, `PASS`, `NAME`, `SEX`, `FONE`, `EMAIL`)"
        ." Values ('$_GET["txtuser"]','$_GET["txtpass"]','$_GET["txtsex"]','$_GET["txtsdt"]','$_GET["txtemail"]','$_GET["txtname"]');";*/

        
        //SQL het nhiem vu
        
        //5. Bat dau xu ly tac vu cua PHP
        mysql_close($conn);//6. dong ket noi den MySQL
}
?>











    
